/***********************************************************************

	This file is part of KEEL-software, the Data Mining tool for regression, 
	classification, clustering, pattern mining and so on.

	Copyright (C) 2004-2010
	
	F. Herrera (herrera@decsai.ugr.es)
    L. Sánchez (luciano@uniovi.es)
    J. Alcalá-Fdez (jalcala@decsai.ugr.es)
    S. García (sglopez@ujaen.es)
    A. Fernández (alberto.fernandez@ujaen.es)
    J. Luengo (julianlm@decsai.ugr.es)

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see http://www.gnu.org/licenses/
  
**********************************************************************/

package keel.GraphInterKeel.datacf.editData;

import keel.GraphInterKeel.datacf.util.Dataset;
import keel.GraphInterKeel.datacf.util.EachRowEditor;
import keel.GraphInterKeel.datacf.util.VariableTable;
import java.util.Vector;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumn;

/**
 * <p>
 * @author Written by Juan Carlos Fernández and Pedro Antonio Gutierrez (University of Cordoba) 23/10/2008
 * @version 1.0
 * @since JDK1.5
 * </p>
 */
public class EditVariablePanel extends javax.swing.JPanel {

    /**
     * <p>
     * Constructor that initializes the panel
     * </p>
     */
    public EditVariablePanel() {
        initComponents();
    }

    /**
     * <p>
     * This method is called from within the constructor to
     * initialize the form.
     * </p>
     * <p>
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     * </p>
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        variablejScrollPane = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        buttonsjPanel = new javax.swing.JPanel();
        addjButton = new javax.swing.JButton();
        deletejButton = new javax.swing.JButton();
        valueRangejPanel = new javax.swing.JPanel();
        valueRangejLabel = new javax.swing.JLabel();
        minjTextField = new javax.swing.JTextField();
        comajLabel = new javax.swing.JLabel();
        maxjTextField = new javax.swing.JTextField();
        rightBracketjLabel = new javax.swing.JLabel();
        nominalValuejLabel = new javax.swing.JLabel();
        nominalValuejTextField = new javax.swing.JTextField();
        addNominalValuejButton = new javax.swing.JButton();
        deleteNominalValuejButton = new javax.swing.JButton();
        leftBracketjLabel = new javax.swing.JLabel();
        changejButton = new javax.swing.JButton();
        rangeNominaljComboBox = new javax.swing.JComboBox();

        setName("Form"); // NOI18N

        variablejScrollPane.setName("variablejScrollPane"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Name", "Type", "Range", "Input/Output"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        variablejScrollPane.setViewportView(jTable1);

        buttonsjPanel.setName("buttonsjPanel"); // NOI18N
        buttonsjPanel.setLayout(new java.awt.GridBagLayout());

        addjButton.setText("Add Variable");
        addjButton.setToolTipText("Add new variable");
        addjButton.setEnabled(false);
        addjButton.setMaximumSize(new java.awt.Dimension(105, 23));
        addjButton.setMinimumSize(new java.awt.Dimension(105, 23));
        addjButton.setName("addjButton"); // NOI18N
        addjButton.setPreferredSize(new java.awt.Dimension(109, 23));
        addjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addjButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.ipadx = 4;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
        buttonsjPanel.add(addjButton, gridBagConstraints);

        deletejButton.setText("Delete Variable");
        deletejButton.setToolTipText("Delete variable");
        deletejButton.setEnabled(false);
        deletejButton.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        deletejButton.setName("deletejButton"); // NOI18N
        deletejButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deletejButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        buttonsjPanel.add(deletejButton, gridBagConstraints);

        valueRangejPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Value Range and Nominal Values"));
        valueRangejPanel.setName("valueRangejPanel"); // NOI18N

        valueRangejLabel.setText("Value Range:");
        valueRangejLabel.setEnabled(false);
        valueRangejLabel.setName("valueRangejLabel"); // NOI18N

        minjTextField.setEnabled(false);
        minjTextField.setName("minjTextField"); // NOI18N
        minjTextField.setPreferredSize(new java.awt.Dimension(60, 20));

        comajLabel.setText(",");
        comajLabel.setEnabled(false);
        comajLabel.setName("comajLabel"); // NOI18N

        maxjTextField.setEnabled(false);
        maxjTextField.setName("maxjTextField"); // NOI18N
        maxjTextField.setPreferredSize(new java.awt.Dimension(60, 20));

        rightBracketjLabel.setText("]");
        rightBracketjLabel.setEnabled(false);
        rightBracketjLabel.setName("rightBracketjLabel"); // NOI18N

        nominalValuejLabel.setText("Nominal Value:");
        nominalValuejLabel.setEnabled(false);
        nominalValuejLabel.setName("nominalValuejLabel"); // NOI18N

        nominalValuejTextField.setEnabled(false);
        nominalValuejTextField.setName("nominalValuejTextField"); // NOI18N

        addNominalValuejButton.setText("Add");
        addNominalValuejButton.setToolTipText("Add new nominal value");
        addNominalValuejButton.setEnabled(false);
        addNominalValuejButton.setName("addNominalValuejButton"); // NOI18N
        addNominalValuejButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNominalValuejButtonActionPerformed(evt);
            }
        });

        deleteNominalValuejButton.setText("Delete");
        deleteNominalValuejButton.setToolTipText("Delete a nominal value");
        deleteNominalValuejButton.setEnabled(false);
        deleteNominalValuejButton.setName("deleteNominalValuejButton"); // NOI18N
        deleteNominalValuejButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteNominalValuejButtonActionPerformed(evt);
            }
        });

        leftBracketjLabel.setText("[");
        leftBracketjLabel.setEnabled(false);
        leftBracketjLabel.setName("leftBracketjLabel"); // NOI18N

        changejButton.setText("Change Range");
        changejButton.setToolTipText("Change Range Values");
        changejButton.setEnabled(false);
        changejButton.setName("changejButton"); // NOI18N
        changejButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changejButtonActionPerformed(evt);
            }
        });

        rangeNominaljComboBox.setEnabled(false);
        rangeNominaljComboBox.setName("rangeNominaljComboBox"); // NOI18N

        javax.swing.GroupLayout valueRangejPanelLayout = new javax.swing.GroupLayout(valueRangejPanel);
        valueRangejPanel.setLayout(valueRangejPanelLayout);
        valueRangejPanelLayout.setHorizontalGroup(
            valueRangejPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(valueRangejPanelLayout.createSequentialGroup()
                .addGroup(valueRangejPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(valueRangejPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(valueRangejPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(valueRangejPanelLayout.createSequentialGroup()
                                .addComponent(leftBracketjLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(minjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(comajLabel))
                            .addComponent(valueRangejLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(maxjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rightBracketjLabel))
                    .addGroup(valueRangejPanelLayout.createSequentialGroup()
                        .addGap(67, 67, 67)
                        .addComponent(changejButton)))
                .addGroup(valueRangejPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(valueRangejPanelLayout.createSequentialGroup()
                        .addGap(63, 63, 63)
                        .addComponent(nominalValuejLabel))
                    .addGroup(valueRangejPanelLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(valueRangejPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(rangeNominaljComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(nominalValuejTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(valueRangejPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(deleteNominalValuejButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(addNominalValuejButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(39, Short.MAX_VALUE))
        );
        valueRangejPanelLayout.setVerticalGroup(
            valueRangejPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(valueRangejPanelLayout.createSequentialGroup()
                .addGroup(valueRangejPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(valueRangejPanelLayout.createSequentialGroup()
                        .addComponent(valueRangejLabel)
                        .addGap(9, 9, 9)
                        .addGroup(valueRangejPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(leftBracketjLabel)
                            .addComponent(minjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(comajLabel)
                            .addComponent(maxjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(rightBracketjLabel)
                            .addComponent(nominalValuejTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(addNominalValuejButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(valueRangejPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(changejButton)
                            .addComponent(rangeNominaljComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(deleteNominalValuejButton)))
                    .addComponent(nominalValuejLabel))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(buttonsjPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE)
            .addComponent(variablejScrollPane, 0, 0, Short.MAX_VALUE)
            .addComponent(valueRangejPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(variablejScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(valueRangejPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonsjPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void deletejButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deletejButtonActionPerformed

    this.deletejButton.setEnabled(false);

    int column, i, j;
    int temp = 0;
    Object nuevo[][];
    String nombres[];

    column = this.jTable1.getSelectedRow();

    if (this.editDataPanel.getData().getInputs().remove(this.editDataPanel.getData().getAttributeIndex(column))) {
        this.editDataPanel.getData().setNentradas(((this.editDataPanel.getData().getNInputs()) - 1));
    }
    if (this.editDataPanel.getData().getOutputs().remove(this.editDataPanel.getData().getAttributeIndex(column))) {
        this.editDataPanel.getData().setNOutputs(((this.editDataPanel.getData().getNOutputs()) - 1));
    }
    this.editDataPanel.getData().getRanges().remove(column);
    this.editDataPanel.getData().getTypes().remove(column);
    this.editDataPanel.getData().getAttributes().remove(column);
    this.editDataPanel.getData().setNVariables((this.editDataPanel.getData().getNVariables()) - 1);
    for (int x = 0; x < this.editDataPanel.getData().getNData(); x++) {
        this.editDataPanel.getData().getPatternIndex(x).remove(column);
    }
    nuevo = new Object[this.editDataPanel.getTablaDataset().getData().length][((this.editDataPanel.getTablaDataset().getData()[0].length) - 1)];

    for (i = 0; i < nuevo.length; i++) {
        temp = 0;
        for (j = 0; j < nuevo[i].length; j++) {
            if (j == column && temp == 0) {
                temp++;
            }
            nuevo[i][j] = (this.editDataPanel.getTablaDataset().getData())[i][j + temp];
        }
    }

    temp = 0;
    nombres = new String[((this.editDataPanel.getTablaDataset().getColumnCount()) - 1)];
    for (i = 0; i < nombres.length; i++) {
        if (i == column && temp == 0) {
            temp++;
        }
        nombres[i] = new String(this.editDataPanel.getTablaDataset().getColumnName(i + temp));
    }

    this.editDataPanel.getTablaDataset().setData(nuevo);
    this.editDataPanel.getTablaDataset().setColumnNames(nombres);
    this.editDataPanel.getTablaDataset().setDatos(this.editDataPanel.getData());
    this.editDataPanel.getScrollPane().getViewport().remove(this.editDataPanel.getJTable1());
    this.editDataPanel.setJTable1(new JTable(this.editDataPanel.getTablaDataset()));
    this.editDataPanel.getJTable1().getTableHeader().setReorderingAllowed(false);
    this.editDataPanel.getJTable1().setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    this.editDataPanel.getJTable1().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    this.editDataPanel.getJTable1().setCellSelectionEnabled(true);
    this.editDataPanel.getJTable1().setColumnSelectionAllowed(true);

    //Refresh EditVariablePanel
    this.refreshVariablePanel(this.editDataPanel.getData());
    //Refresh EditDataPanel
    this.editDataPanel.refreshDataPanel(this.editDataPanel.getData());

    //Disable buttons
    this.enabledRanges(false);
}//GEN-LAST:event_deletejButtonActionPerformed

private void addNominalValuejButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addNominalValuejButtonActionPerformed

    this.deletejButton.setEnabled(false);
    int row = jTable1.getSelectedRow();

    String nuevo = new String(this.nominalValuejTextField.getText());
    nuevo = (this.eliminateSpaces(nuevo));
    if (nuevo.trim().equals("") == false) {
        this.nominalValuejTextField.setText("");

        ((Vector) (this.editDataPanel.getData().getRanges().elementAt(row))).add(nuevo);

        JComboBox comboBox = new JComboBox();
        for (int i = 0; i < this.editDataPanel.getData().getRange(row).size(); i++) {
            comboBox.addItem(new String(this.editDataPanel.getData().getRangesEnum(row, i)));
        }

        EachRowEditor rowEditor = new EachRowEditor(jTable1);
        rowEditor.setEditorAt(row, new DefaultCellEditor(comboBox));
        this.jTable1.getColumnModel().getColumn(2).setCellEditor(rowEditor);
    }

    //Refresh EditVariablePanel
    this.refreshVariablePanel(this.editDataPanel.getData());

    //Disable buttons
    this.enabledRanges(false);

    //Refresh EditDataPanel
    this.editDataPanel.refreshDataPanel(this.editDataPanel.getData());
}//GEN-LAST:event_addNominalValuejButtonActionPerformed

private void deleteNominalValuejButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteNominalValuejButtonActionPerformed

    int row = jTable1.getSelectedRow();
    int i;

    if (this.rangeNominaljComboBox.getSelectedIndex() != -1) {

        Vector aux;
        Vector<String> aux2;
        for (int w = 0; w < this.editDataPanel.getData().getDataVector().size(); w++) {
            aux = this.editDataPanel.getData().getDataVector();
            aux2 = ((Vector<String>) aux.get(w));
            if (aux2.get(row).equals(this.rangeNominaljComboBox.getSelectedItem())) {
                aux2.setElementAt("<null>", row);
            }
        }


        ((Vector) (this.editDataPanel.getData().getRanges().elementAt(row))).remove(new String((String) (this.rangeNominaljComboBox.getSelectedItem())));
        this.rangeNominaljComboBox.removeItemAt(this.rangeNominaljComboBox.getSelectedIndex());






        JComboBox comboBox = new JComboBox();
        for (i = 0; i < this.editDataPanel.getData().getRange(row).size(); i++) {
            comboBox.addItem(new String(this.editDataPanel.getData().getRangesEnum(row, i)));
        }

        EachRowEditor rowEditor = new EachRowEditor(jTable1);
        rowEditor.setEditorAt(row, new DefaultCellEditor(comboBox));
        this.jTable1.getColumnModel().getColumn(2).setCellEditor(rowEditor);
    }



    //Refresh EditVariablePanel
    this.refreshVariablePanel(this.editDataPanel.getData());

    //Refresh EditDataPanel
    this.editDataPanel.refreshDataPanel(this.editDataPanel.getData());

    //Disable buttons
    this.enabledRanges(false);

    //if there is not ranges
    if (this.editDataPanel.getData().getRange(row).size() == 0) {
        JOptionPane.showMessageDialog(this, "Please, insert new nominal values for the variable " + "\"" + this.editDataPanel.getData().getAttributeIndex(row) + "\"", "Variable without range", 2);
    }
}//GEN-LAST:event_deleteNominalValuejButtonActionPerformed

private void changejButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changejButtonActionPerformed
    int row = 0;

    try {
        row = jTable1.getSelectedRow();
        Vector r = new Vector();
        r = (Vector) this.editDataPanel.getData().getRanges().elementAt(row);

        if (this.editDataPanel.getData().getAttributeTypeIndex(row).equalsIgnoreCase("integer")) {
            int inferior = Integer.parseInt(minjTextField.getText());
            int superior = Integer.parseInt(maxjTextField.getText());
            Integer auxI = (Integer) r.get(0);
            Integer auxS = (Integer) r.get(1);
            if (inferior > auxI.intValue()) {
                JOptionPane.showMessageDialog(this, "Incorrect Range", "Error", 2);
                this.minjTextField.setText(auxI.toString());
                this.maxjTextField.setText(auxS.toString());
                return;
                /*
                for(int i=0; i<this.editDataPanel.getData().getNData(); i++)
                {
                String myString = this.editDataPanel.getData().getPatternIndex(i, row);
                if(Integer.valueOf(myString).intValue()<inferior)
                this.editDataPanel.tablaDataset.setValueAt(inferior, i, row);
                }
                 */
            }

            if (superior < auxS.intValue()) {
                JOptionPane.showMessageDialog(this, "Incorrect Range", "Error", 2);
                this.minjTextField.setText(auxI.toString());
                this.maxjTextField.setText(auxS.toString());
                return;
                /*
                for(int i=0; i<this.editDataPanel.getData().getNData(); i++)
                {
                String myString = this.editDataPanel.getData().getPatternIndex(i, row);
                if(Integer.valueOf(myString).intValue()>superior)
                this.editDataPanel.tablaDataset.setValueAt(inferior, i, row);
                }
                 */
            }
        } else {
            double inferior = Double.parseDouble(minjTextField.getText());
            double superior = Double.parseDouble(maxjTextField.getText());
            Double auxI = (Double) r.get(0);
            Double auxS = (Double) r.get(1);
            if (inferior > auxI.doubleValue()) {
                JOptionPane.showMessageDialog(this, "Incorrect Range", "Error", 2);
                this.minjTextField.setText(auxI.toString());
                this.maxjTextField.setText(auxS.toString());
                return;
                /*
                for(int i=0; i<this.editDataPanel.getData().getNData(); i++)
                {
                String myString = this.editDataPanel.getData().getPatternIndex(i, row);
                if(Integer.valueOf(myString).intValue()<inferior)
                this.editDataPanel.tablaDataset.setValueAt(inferior, i, row);
                }
                 */
            }

            if (superior < auxS.doubleValue()) {
                JOptionPane.showMessageDialog(this, "Incorrect Range", "Error", 2);
                this.minjTextField.setText(auxI.toString());
                this.maxjTextField.setText(auxS.toString());
                return;
                /*
                for(int i=0; i<this.editDataPanel.getData().getNData(); i++)
                {
                String myString = this.editDataPanel.getData().getPatternIndex(i, row);
                if(Integer.valueOf(myString).intValue()>superior)
                this.editDataPanel.tablaDataset.setValueAt(inferior, i, row);
                }
                 */
            }
        }
    } catch (NumberFormatException ex) {
        JOptionPane.showMessageDialog(this, "Please, insert a correct number", "Error", 2);
    }

    //Minimum!!!
    try {
        row = jTable1.getSelectedRow();
        int n;
        double m;
        if (this.editDataPanel.getData().getAttributeTypeIndex(row).equalsIgnoreCase("integer")) {
            n = Integer.parseInt(minjTextField.getText());

            if (n > Integer.parseInt(maxjTextField.getText())) {
                JOptionPane.showMessageDialog(this, "Please, insert a minimum number less than maximum", "Error", 2);
                return;
            }
            ((Vector) (this.editDataPanel.getData().getRanges().elementAt(row))).removeAllElements();
            ((Vector) (this.editDataPanel.getData().getRanges().elementAt(row))).add(new Integer(minjTextField.getText()));
            ((Vector) (this.editDataPanel.getData().getRanges().elementAt(row))).add(new Integer(maxjTextField.getText()));
        } else {
            m = Double.parseDouble(minjTextField.getText());
            if (m > Double.parseDouble(maxjTextField.getText())) {
                JOptionPane.showMessageDialog(this, "Please, insert a minimum number less than maximum", "Error", 2);
                return;
            }
            ((Vector) (this.editDataPanel.getData().getRanges().elementAt(row))).removeAllElements();
            ((Vector) (this.editDataPanel.getData().getRanges().elementAt(row))).add(new Double(minjTextField.getText()));
            ((Vector) (this.editDataPanel.getData().getRanges().elementAt(row))).add(new Double(maxjTextField.getText()));
        }
    } catch (NumberFormatException ex) {
        System.out.println("EXCEPCION 1!!!!!!!!!!!!!!!!");
        JOptionPane.showMessageDialog(this, "Please, insert a correct number", "Error", 2);
        return;
    }

    //MAximum!!!
    try {
        row = jTable1.getSelectedRow();
        int n;
        double m;

        if (this.editDataPanel.getData().getAttributeTypeIndex(row).equalsIgnoreCase("integer")) {
            n = Integer.parseInt(maxjTextField.getText());
            if (n < Integer.parseInt(minjTextField.getText())) {
                JOptionPane.showMessageDialog(this, "Please, insert a number greater than min Interval", "Error", 2);
                return;
            }
            ((Vector) (this.editDataPanel.getData().getRanges().elementAt(row))).removeAllElements();
            ((Vector) (this.editDataPanel.getData().getRanges().elementAt(row))).add(new Integer(minjTextField.getText()));
            ((Vector) (this.editDataPanel.getData().getRanges().elementAt(row))).add(new Integer(maxjTextField.getText()));
        } else {
            m = Double.parseDouble(maxjTextField.getText());
            if (m < Double.parseDouble(minjTextField.getText())) {
                JOptionPane.showMessageDialog(this, "Please, insert a number greater than min Interval", "Error", 2);
                return;
            }
            ((Vector) (this.editDataPanel.getData().getRanges().elementAt(row))).removeAllElements();
            ((Vector) (this.editDataPanel.getData().getRanges().elementAt(row))).add(new Double(minjTextField.getText()));
            ((Vector) (this.editDataPanel.getData().getRanges().elementAt(row))).add(new Double(maxjTextField.getText()));
        }
    } catch (NumberFormatException ex) {
        System.out.println("EXCEPCION 2!!!!!!!!!!!!!!!!");
        JOptionPane.showMessageDialog(this, "Please, insert a correct number", "Error", 2);
    }

    //Disable buttons
    this.enabledRanges(false);

    //Refresh EditVariablePanel
    this.refreshVariablePanel(this.editDataPanel.getData());

    //Refresh EditDataPanel
    this.editDataPanel.refreshDataPanel(this.editDataPanel.getData());

}//GEN-LAST:event_changejButtonActionPerformed

private void addjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addjButtonActionPerformed

    this.counter++;
    //Se aÃ±ade la nueva variable
    this.editDataPanel.getData().getInputs().addElement(new String("newInput" + counter)); //Nueva variable input
    this.editDataPanel.getData().setNentradas(((this.editDataPanel.getData().getNInputs()) + 1));
    this.editDataPanel.getData().setNVariables((this.editDataPanel.getData().getNVariables()) + 1);
    Vector rangos = new Vector();
    rangos.add(new Integer(0));
    rangos.add(new Integer(0));
    this.editDataPanel.getData().getRanges().addElement(rangos);
    this.editDataPanel.getData().getTypes().addElement(new String("integer"));
    this.editDataPanel.getData().getAttributes().addElement(new String("newInput" + counter));


    Vector aux;
    Vector<String> aux2;
    for (int i = 0; i < editDataPanel.getData().getDataVector().size(); i++) {
        aux = this.editDataPanel.getData().getDataVector();
        aux2 = ((Vector<String>) aux.get(i));
        aux2.addElement("0");
    }

    //Refresh EditVariablePanel
    this.refreshVariablePanel(this.editDataPanel.getData());
    //Refresh EditDataPanel
    this.editDataPanel.refreshDataPanel(this.editDataPanel.getData());

    //Disable buttons
    this.enabledRanges(false);
}//GEN-LAST:event_addjButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addNominalValuejButton;
    private javax.swing.JButton addjButton;
    private javax.swing.JPanel buttonsjPanel;
    private javax.swing.JButton changejButton;
    private javax.swing.JLabel comajLabel;
    private javax.swing.JButton deleteNominalValuejButton;
    private javax.swing.JButton deletejButton;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel leftBracketjLabel;
    private javax.swing.JTextField maxjTextField;
    private javax.swing.JTextField minjTextField;
    private javax.swing.JLabel nominalValuejLabel;
    private javax.swing.JTextField nominalValuejTextField;
    private javax.swing.JComboBox rangeNominaljComboBox;
    private javax.swing.JLabel rightBracketjLabel;
    private javax.swing.JLabel valueRangejLabel;
    private javax.swing.JPanel valueRangejPanel;
    private javax.swing.JScrollPane variablejScrollPane;
    // End of variables declaration//GEN-END:variables

    // Edit Data Panel
    protected EditDataPanel editDataPanel = null;

    //Table for Variables
    protected VariableTable variableTable = null;

    // Count new variables added
    protected int counter = 0;

    /**
     * <p>
     * Set Edit Data Panel
     * </p>
     * @param editDataPanel Panel for editing data
     */
    public void setEditDataPanel(EditDataPanel editDataPanel) {
        this.editDataPanel = editDataPanel;
    }

    /**
     * <p>
     * Get Edit Data Panel
     * </p>
     * @return EditDataPanel The panel for editing data
     */
    public EditDataPanel getEditDataPanel() {
        return this.editDataPanel;
    }

    /**
     * <p>
     * Sets the table including the variables
     * </p>
     * @param table New VariableTable
     */
    public void setVariableTable(VariableTable table) {
        this.variableTable = table;
    }

    /**
     * <p>
     * Gets the current VariableTable
     * </p>
     * @return Current VariableTable
     */
    public VariableTable getVariableTable() {
        return this.variableTable;
    }

    /**
     * <p>
     * Gets the current JTable
     * </p>
     * @return Current JTable
     */
    public JTable getJTable() {
        return this.jTable1;
    }

    /**
     * <p>
     * Sets a new JTable
     * </p>
     * @param jtable The new JTable
     */
    public void setJTable(JTable jtable) {
        this.jTable1 = jtable;
    }

    /**
     * <p>
     * Method for eliminating the spaces of a string
     * </p>
     * @param in string to be processed
     * @return String Processed String
     */
    private String eliminateSpaces(String in) {
        String out = new String();
        String cachos[] = in.split(" ");

        for (int i = 0; i < cachos.length; i++) {
            out = out.concat(cachos[i]);
        }

        return out;
    }

    /**
     * <p>
     * Sets the counter value
     * </p>
     * @param value New value
     */
    public void setCounter(int value) {
        this.counter = value;
    }

    /**
     * <p>
     * Set if addButton is enabled or disabled
     * </p>
     * @param value boolean indicating if the button is enabled or disabled
     */
    public void setStateAddButton(boolean value) {
        this.addjButton.setEnabled(value);
    }

    /**
     * <p>
     * Enables or Disables Delete Button
     * </p>
     * @param state boolean indicating if the button is enabled or disabled
     */
    public void setStateDeleteButton(boolean state) {
        this.deletejButton.setEnabled(state);
    }

    /**
     * <p>
     * Refreshes the variable panel
     * </p>
     * @param dataSet New modified dataset
     */
    public void refreshVariablePanel(Dataset dataSet) {
        this.variableTable = new VariableTable(dataSet, this);
        this.jTable1.setModel(this.variableTable);
        this.jTable1.setFont(new java.awt.Font("Arial", 0, 11));
        this.jTable1.getTableHeader().setReorderingAllowed(false);
        this.jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        this.jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.jTable1.setCellSelectionEnabled(true);
        this.jTable1.setColumnSelectionAllowed(true);

        this.rangeNominaljComboBox.removeAllItems();
        this.minjTextField.setText("");
        this.maxjTextField.setText("");

        EachRowEditor rowEditor = new EachRowEditor(jTable1);
        //Rellena con combo las casillas de Range en caso de que sea Nominal
        for (int i = 0; i < dataSet.getNVariables(); i++) {
            if (dataSet.getAttributeTypeIndex(i).equalsIgnoreCase("nominal")) {
                Vector<String> values = new Vector();
                for (int j = 0; j < dataSet.getRange(i).size(); j++) {
                    values.add(new String(dataSet.getRangesEnum(i, j)));
                }
                JComboBox combo = new JComboBox(values);
                rowEditor.setEditorAt(i, new DefaultCellEditor(combo));
            }
            jTable1.getColumnModel().getColumn(2).setCellEditor(rowEditor);
        }

        //Rellena con combo las casillas Input/Ouput
        TableColumn combo1 = this.jTable1.getColumnModel().getColumn(3);
        JComboBox comboBox1 = new JComboBox();
        comboBox1.addItem(new String("Input"));
        comboBox1.addItem(new String("Output"));
        combo1.setCellEditor(new DefaultCellEditor(comboBox1));

        //Rellena con combo las casillas Type
        TableColumn combo2 = this.jTable1.getColumnModel().getColumn(1);
        JComboBox comboBox2 = new JComboBox();
        comboBox2.addItem(new String("integer"));
        comboBox2.addItem(new String("real"));
        comboBox2.addItem(new String("nominal"));
        combo2.setCellEditor(new DefaultCellEditor(comboBox2));

        SelectionListener listener = new SelectionListener();
        jTable1.getSelectionModel().addListSelectionListener(listener);
        jTable1.getColumnModel().getSelectionModel().addListSelectionListener(listener);

        this.variablejScrollPane.getViewport().remove(this.jTable1);
        this.variablejScrollPane.getViewport().add(this.jTable1);

    }

    /**
     * <p>
     * Set if the range button is enabled or disabled
     * </p>
     * @param value boolean indicating if the button is enabled or disabled
     */
    public void enabledRanges(boolean value) {
        nominalValuejLabel.setEnabled(value);
        nominalValuejTextField.setEnabled(value);
        rangeNominaljComboBox.setEnabled(value);
        addNominalValuejButton.setEnabled(value);
        deleteNominalValuejButton.setEnabled(value);

        valueRangejLabel.setEnabled(value);
        minjTextField.setEnabled(value);
        comajLabel.setEnabled(value);
        maxjTextField.setEnabled(value);
        rightBracketjLabel.setEnabled(value);
        leftBracketjLabel.setEnabled(value);
        changejButton.setEnabled(value);

    }

    // Private listener for rows
    private class SelectionListener implements ListSelectionListener {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            // If cell selection is enabled, both column and column change events are fired
            if (!e.getValueIsAdjusting() && e.getSource() == jTable1.getSelectionModel() && jTable1.getRowSelectionAllowed() && jTable1.getColumnSelectionAllowed()) {
                // Row selection changed
                ListSelectionModel csm = (ListSelectionModel) e.getSource();
                if (!csm.isSelectionEmpty()) {
                    deletejButton.setEnabled(true);
                    int row = csm.getMinSelectionIndex();

                    if (row >= 0) {
                        if (editDataPanel.getData().getAttributeTypeIndex(row).equalsIgnoreCase("nominal")) {
                            nominalValuejLabel.setEnabled(true);
                            nominalValuejTextField.setEnabled(true);
                            rangeNominaljComboBox.setEnabled(true);
                            addNominalValuejButton.setEnabled(true);
                            deleteNominalValuejButton.setEnabled(true);

                            valueRangejLabel.setEnabled(false);
                            minjTextField.setEnabled(false);
                            comajLabel.setEnabled(false);
                            maxjTextField.setEnabled(false);
                            rightBracketjLabel.setEnabled(false);
                            leftBracketjLabel.setEnabled(false);
                            changejButton.setEnabled(false);

                            rangeNominaljComboBox.removeAllItems();
                            for (int i = 0; i < editDataPanel.getData().getRange(row).size(); i++) {
                                rangeNominaljComboBox.addItem(new String(editDataPanel.getData().getRangesEnum(row, i)));
                            }
                        } else {
                            nominalValuejLabel.setEnabled(false);
                            nominalValuejTextField.setEnabled(false);
                            rangeNominaljComboBox.setEnabled(false);
                            addNominalValuejButton.setEnabled(false);
                            deleteNominalValuejButton.setEnabled(false);

                            valueRangejLabel.setEnabled(true);
                            minjTextField.setEnabled(true);
                            comajLabel.setEnabled(true);
                            maxjTextField.setEnabled(true);
                            rightBracketjLabel.setEnabled(true);
                            leftBracketjLabel.setEnabled(true);
                            changejButton.setEnabled(true);

                            if (editDataPanel.getData().getAttributeTypeIndex(row).equalsIgnoreCase("integer")) {
                                minjTextField.setText(editDataPanel.getData().getRangesInt(row, 0).toString());
                                maxjTextField.setText(editDataPanel.getData().getRangesInt(row, 1).toString());
                            } else {
                                minjTextField.setText(editDataPanel.getData().getRangesReal(row, 0).toString());
                                maxjTextField.setText(editDataPanel.getData().getRangesReal(row, 1).toString());
                            }
                        }
                    }
                } else {
                }
            } else // e.getValueIsAdjusting() --> true. Se estan metiendo datos en una variable nueva
            {
                //
            }
        }
    }
}

